package com.zlw.rabbitmq.message;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/12/22 10:37
 */
public class MessageProduct {


    public static void main(String[] args) throws IOException, TimeoutException {


        //1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setVirtualHost("/");
        connectionFactory.setHost("39.100.250.122");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");


        //2. 通过连接工厂 创建连接。
        Connection connection = connectionFactory.newConnection();

        //3. 通过connection 创建channel
        Channel channel = connection.createChannel();

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("one", "one");
        hashMap.put("two", "two");
        AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
                .deliveryMode(2)
                .contentEncoding("UTF-8")
                .expiration("10000")
                .correlationId("")
                .headers(hashMap)
                .build();

        //4. 消息发送
        for (int i = 0; i < 5; i++) {

            String msg = "你好 RabbitMQ !  message --> " + i;
            //不指定交换机的名称 rabbitmq的匹配规则就是 把routingkey和队列名称进行匹配。
            channel.basicPublish("", "test001", properties, msg.getBytes());
        }

        //5. 关闭相关连接
        channel.close();
        connection.close();

    }


}
